project(BIG2)

add_shaders_directory(shaders SHADERS_TARGET_NAME)

set(BIG2_SOURCES)

# include/
list(APPEND BIG2_SOURCES include/big2/bgfx/bgfx_callback_handler.h)
list(APPEND BIG2_SOURCES include/big2/bgfx/bgfx_frame_buffer_scoped.h)
list(APPEND BIG2_SOURCES include/big2/bgfx/embedded_shader.h)
list(APPEND BIG2_SOURCES include/big2/bgfx/bgfx_frame_buffer_scoped.h)
list(APPEND BIG2_SOURCES include/big2/bgfx/bgfx_view_scoped.h)
list(APPEND BIG2_SOURCES include/big2/bgfx/bgfx_scoped_handle.h)
list(APPEND BIG2_SOURCES include/big2/bgfx/bgfx_utils.h)
list(APPEND BIG2_SOURCES include/big2/app.h)
list(APPEND BIG2_SOURCES include/big2/simple_app.h)
list(APPEND BIG2_SOURCES include/big2/execution.h)
list(APPEND BIG2_SOURCES include/big2/algorithm.h)
list(APPEND BIG2_SOURCES include/big2/app_extension_base.h)
list(APPEND BIG2_SOURCES include/big2/default_quit_condition_app_extension.h)
list(APPEND BIG2_SOURCES include/big2/imgui/imgui_app_extension.h)
list(APPEND BIG2_SOURCES include/big2/event_queue.h)
list(APPEND BIG2_SOURCES include/big2/glfw/glfw_utils.h)
list(APPEND BIG2_SOURCES include/big2/glfw/glfw_initialization_scoped.h)
list(APPEND BIG2_SOURCES include/big2/macros.h)
list(APPEND BIG2_SOURCES include/big2/void_ptr.h)
list(APPEND BIG2_SOURCES include/big2/asserts.h)
list(APPEND BIG2_SOURCES include/big2.h)

# src/
list(APPEND BIG2_SOURCES src/glfw/glfw_utils.cpp)
list(APPEND BIG2_SOURCES src/glfw/glfw_initialization_scoped.cpp)
list(APPEND BIG2_SOURCES src/window.cpp)
list(APPEND BIG2_SOURCES src/bgfx/bgfx_callback_handler.cpp)
list(APPEND BIG2_SOURCES src/bgfx/bgfx_initialization_scoped.cpp)
list(APPEND BIG2_SOURCES src/bgfx/bgfx_utils.cpp)
list(APPEND BIG2_SOURCES src/bgfx/bgfx_frame_buffer_scoped.cpp)
list(APPEND BIG2_SOURCES src/bgfx/bgfx_view_scoped.cpp)
list(APPEND BIG2_SOURCES src/event_queue.cpp)
list(APPEND BIG2_SOURCES src/app.cpp)
list(APPEND BIG2_SOURCES src/app_extension_base.cpp)
list(APPEND BIG2_SOURCES src/default_quit_condition_app_extension.cpp)
list(APPEND BIG2_SOURCES src/imgui/imgui_app_extension.cpp)
list(APPEND BIG2_SOURCES src/native_window.h)
list(APPEND BIG2_SOURCES src/id_manager.h)
list(APPEND BIG2_SOURCES src/asserts.cpp)
list(APPEND BIG2_SOURCES src/simple_app.cpp)
list(APPEND BIG2_SOURCES src/big2.cpp)

if ("${BIG2_INCLUDE_IMGUI}")
    list(APPEND BIG2_SOURCES include/big2/imgui/imgui.h)
    list(APPEND BIG2_SOURCES include/big2/imgui/imgui_impl_bgfx.h)
    list(APPEND BIG2_SOURCES include/big2/imgui/imgui_frame_scoped.h)
    list(APPEND BIG2_SOURCES include/big2/imgui/imgui_context_wrapper.h)

    list(APPEND BIG2_SOURCES src/imgui/imgui.cpp)
    list(APPEND BIG2_SOURCES src/imgui/imgui_frame_scoped.cpp)
    list(APPEND BIG2_SOURCES src/imgui/imgui_context_wrapper.cpp)
    list(APPEND BIG2_SOURCES src/imgui/imgui_impl_bgfx.cpp)
endif ()

add_library(${PROJECT_NAME} ${BIG2_SOURCES})
target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src")
target_link_libraries(${PROJECT_NAME} PUBLIC glfw bx bimg bgfx)
target_link_libraries(${PROJECT_NAME} PUBLIC Microsoft.GSL::GSL)
target_link_libraries(${PROJECT_NAME} PUBLIC glm)
target_link_libraries(${PROJECT_NAME} PRIVATE spdlog)
target_link_libraries(${PROJECT_NAME} PUBLIC "${SHADERS_TARGET_NAME}")

if ("${BIG2_INCLUDE_IMGUI}")
    target_link_libraries(${PROJECT_NAME} PUBLIC imgui)
    target_compile_definitions(${PROJECT_NAME} PUBLIC BIG2_IMGUI_ENABLED=1)
else ()
    target_compile_definitions(${PROJECT_NAME} PUBLIC BIG2_IMGUI_ENABLED=0)
endif ()

target_compile_definitions(${PROJECT_NAME} PUBLIC GLFW_INCLUDE_NONE)
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_20)

if (CMAKE_COMPILER_IS_GNUCC AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 10)

    find_package(TBB)
    if(TBB_FOUND)
        target_compile_definitions(${PROJECT_NAME} PUBLIC BIG2_EXECUTION_ENABLED=1)
        target_link_libraries(${PROJECT_NAME} PUBLIC tbb)
    else()
        message(STATUS "Compiling without parallel execution support")
        target_compile_definitions(${PROJECT_NAME} PUBLIC BIG2_EXECUTION_ENABLED=0)
    endif()

else()
    target_compile_definitions(${PROJECT_NAME} PUBLIC BIG2_EXECUTION_ENABLED=1)
endif()

if (${BIG2_PROGRAMMER})

    set(WARNING_OPTIONS)
    if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
        list(APPEND WARNING_OPTIONS "-Wall")
        list(APPEND WARNING_OPTIONS "-Wextra")
        list(APPEND WARNING_OPTIONS "-Wno-c++98-compat")
        list(APPEND WARNING_OPTIONS "-Wdocumentation")
    elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
        list(APPEND WARNING_OPTIONS "-Wall")
        list(APPEND WARNING_OPTIONS "-Wextra")
        list(APPEND WARNING_OPTIONS "-Wno-unknown-pragmas")
    elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
        list(APPEND WARNING_OPTIONS "/W4")
    endif ()

    target_compile_options(${PROJECT_NAME} PRIVATE ${WARNING_OPTIONS})

    set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_WARNING_AS_ERROR FALSE)
endif ()
